package com.zms.maze;

import static java.lang.Math.*;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Line implements Comparable<Line>,Serializable {

	private static final long serialVersionUID = 1L;
	
	public int x1;
	public int y1;
	public int x2;
	public int y2;
	
	public Line()
	{
		
	}
	
	public Line(int x1,int y1,int x2,int y2) {
		this.x1=min(x1,x2);
		this.y1=min(y1,y2);
		this.x2=max(x2,x1);
		this.y2=max(y2,y1);
	}
	public boolean isVertical() {
		return x1==x2;
	}
	public int compareTo(Line another) {
		if(this.isVertical()!=another.isVertical()) {
			if(this.isVertical()) {
				if(this.x1==another.x1 && this.y1==another.y1) {
					return 1;
				}
				if(this.x2==another.x1 && this.y2==another.y1) {
					return -1;					
				}
				if(this.x1==another.x2 && this.y1==another.y2) {
					return 1;
				}
				if(this.x2==another.x2 && this.y2==another.y2) {
					return -1;
				}
				return 0;
			}			
			return -another.compareTo(this);
		}	
		if(this.isVertical() && another.isVertical()) {
			if(this.x1==another.x1) {
				if(this.y2==another.y1) {
					return -1;
				}
				if(another.y2==this.y1) {
					return 1;
				}
			}
		}
		return 0;
	}
	public void writeObject(ObjectOutputStream out) throws IOException {		
		out.writeInt(x1);
		out.writeInt(y1);
		out.writeInt(x2);
		out.writeInt(y2);
	}
	public static Line readObject(ObjectInputStream in) throws IOException,ClassNotFoundException {
		return new Line(in.readInt(),in.readInt(),in.readInt(),in.readInt());
	}
	public String toString() {
		return String.valueOf(x1)+String.valueOf(y1)+String.valueOf(x2)+String.valueOf(y2);
	}
	public boolean equals(Line line) {
		return this.x1==line.x1 && this.x2==line.x2 && this.y1==line.y1 && this.y2==line.y2;
	}
}
